package nic.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import nic.controller.Connection;
import nic.controller.QNapController;

public class DownloadServerDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JTextField ipAddressJTextField = null;
	private JTextField portJTextField = null;
	private JLabel portJLabel = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JTextField usernameJTextField = null;
	private JTextField passwordJTextField = null;
	private boolean ok = false;
	private JButton okJButton = null;
	private QNapController controller = null;
	private JCheckBox doSaveCredentials = null;
	
	/**
	 * This method initializes 
	 * 
	 */
	public DownloadServerDialog(JFrame fr, QNapController controller) {
		super(fr, "Specify download server", false);
		this.controller = controller;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new Dimension(434, 250));
        this.setModal(true);
        this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.BOTH;
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 6;
			
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridx = 0;
			
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridy = 7;
			
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			//gridBagConstraints9.fill = GridBagConstraints.BOTH;
			gridBagConstraints9.gridy = 9;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.gridx = 0;
			
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.fill = GridBagConstraints.BOTH;
			gridBagConstraints8.gridy = 8;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.gridx = 0;
			portJLabel = new JLabel();
			portJLabel.setText("Port (usually 8080 for TS-109/TS-209 and 6000 for TS-101/TS-201/TS-100)");
			GridBagConstraints gridBagConstraints0 = new GridBagConstraints();
			gridBagConstraints0.fill = GridBagConstraints.BOTH;
			gridBagConstraints0.gridx = 0;
			gridBagConstraints0.gridy = 0;
			jLabel3 = new JLabel();
			jLabel3.setText("Password");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 5;
			jLabel2 = new JLabel();
			jLabel2.setText("Username");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 4;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridy = 3;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.gridx = 0;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 2;
			jLabel = new JLabel();
			jLabel.setText("Download server address (example: 192.168.1.22)");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(jLabel, gridBagConstraints0);
			jContentPane.add(getIpAddressJTextField(), gridBagConstraints1);
			jContentPane.add(portJLabel, gridBagConstraints2);
			jContentPane.add(getPortJTextField(), gridBagConstraints3);
			
			jContentPane.add(jLabel2, gridBagConstraints4);
			jContentPane.add(getUsernameJTextField(), gridBagConstraints5);
			jContentPane.add(jLabel3, gridBagConstraints6);
			jContentPane.add(getPasswordJTextField(), gridBagConstraints7);
			jContentPane.add(getDoSaveCredentials(), gridBagConstraints8);

			jContentPane.add(getOkJButton(), gridBagConstraints9);
			this.getRootPane().setDefaultButton(getOkJButton());
		}
		return jContentPane;
	}

	/**
	 * This method initializes urlJTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getIpAddressJTextField() {
		if (ipAddressJTextField == null) {
			ipAddressJTextField = new JTextField();
			ipAddressJTextField.setFocusable(true);
			ipAddressJTextField.setText(this.controller.connection.getServerAddress());
		}
		return ipAddressJTextField;
	}
	
	/**
	 * This method initializes urlJTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPortJTextField() {
		if (portJTextField == null) {
			portJTextField = new JTextField();
			portJTextField.setFocusable(true);
			if (this.controller.connection.getPort() >= 0)
			{
				portJTextField.setText(Integer.toString(this.controller.connection.getPort()));
			}
		}
		return portJTextField;
	}

	/**
	 * This method initializes usernameJTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUsernameJTextField() {
		if (usernameJTextField == null) {
			usernameJTextField = new JTextField();
			usernameJTextField.setFocusable(true);
			usernameJTextField.setText(this.controller.connection.getUsername());
		}
		return usernameJTextField;
	}

	/**
	 * This method initializes passwordJTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPasswordJTextField() {
		if (passwordJTextField == null) {
			passwordJTextField = new JPasswordField();
			((JPasswordField)passwordJTextField).setEchoChar('*');
			passwordJTextField.setFocusable(true);
			passwordJTextField.setText(this.controller.connection.getPassword());
		}
		return passwordJTextField;
	}

	public boolean isOk() {
		return ok;
	}
	
	public String getServerAddress() {
		return getIpAddressJTextField().getText();
	}
	
	public int getPort() {
		return Integer.parseInt(getPortJTextField().getText());
	}
	
	public String getUsername()
	{
		return usernameJTextField.getText();
	}
	
	public String getPassword()
	{
		return passwordJTextField.getText();
	}
	
	/**
	 * This method initializes okJButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkJButton() {
		if (okJButton == null) {
			okJButton = new JButton();
			okJButton.setText("Ok");
			okJButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {

					showDownloadServerDialog();
				}
			});
			okJButton.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed (KeyEvent e){
					if (e.getID() == KeyEvent.VK_ENTER
							|| e.getID() == 401)
					{
						showDownloadServerDialog();
					}
				}
			});
		}
		return okJButton;
	}
	
	private void showDownloadServerDialog() {
		Connection connection = new Connection(getServerAddress(), getPort(), getUsername(), getPassword()); 
		DownloadServerDialog.this.ok = DownloadServerDialog.this.controller.checkServer(connection);
		if (!ok)
		{
	    	JOptionPane.showMessageDialog(DownloadServerDialog.this,
	    			"Could not find server");
	    } else
	    {
	    	setVisible(false);
	    }
	}

	public JCheckBox getDoSaveCredentials() {
		if (doSaveCredentials == null)
		{
			doSaveCredentials = new JCheckBox();
			doSaveCredentials.setText("Remember");
			doSaveCredentials.setSelected(true);
		}
		return doSaveCredentials;
	}

	public boolean doRememberCredentials() {
		return getDoSaveCredentials().isSelected();
	}
}
